<?php
namespace Admin\Controller;

use Think\Controller;

class AdminBaseController extends Controller
{
    public $loginInfo = null;
    public $uid = null;

    public function _initialize()
    {

        $this->_getUserLoginInfo(); //获取登录信息
        $this->checkPower();
        if($this->uid){
            $menuList = $this->getMenuList();
        }
        //print_g($this->getMenuList());
        $this->assign([
            'personMenuList' => $menuList,
            'controller' => strtolower(CONTROLLER_NAME)
        ]);

    }

    /**
     * 检查当前操作权限
     */
    public function checkPower(){

        $allowController = ['Public'];
        $nowController = CONTROLLER_NAME;
        $nowMethod = ACTION_NAME;
        if (!$this->uid){
            if (!in_array($nowController, $allowController)) {  //非登录用户跳转到登录页面
                $this->redirect('public/login');
            }else{
                $this->error('您无权限进行此操作!');
            }
        }
        if($this->loginInfo['role_id'] == 1){ //超级管理员 不需权限检测
            return true;
        }else{
            $rst = M('admin_rule') ->alias('ar')->join('rj_admin_role_rule arr on ar.id = arr.rule_id')
                ->where('arr.role_id = '.$this->loginInfo['role_id'].' and ar.url = "'.$nowController.'/'.$nowMethod.'"')->find();
            if($rst){
                return true;
            }else{
                $this->error('您无权限进行此操作!');
            }
        }
    }

    /**
     * 获取菜单
     */
    public function getMenuList()
    {

        $menuModel = D('Menu');
        return $menuModel->treeList();
    }

    /**
     * 获取登录信息
     * 如果登录 返回登录信息 反之 返回false
     * @return bool|mixed
     */
    protected function _getUserLoginInfo()
    {

        $loginInfo = session('user');
        if ($loginInfo) {
            $this->loginInfo = $loginInfo;
            $this->uid = $loginInfo['uid'];
            return $loginInfo;
        } else {
            return false;
        }

    }

    protected function _display($tpl, $title = '')
    {

        $this->assign([
            'seo_title' => $title . '|' . C('SYS_TITLE'),
            'title' => $title

        ]);
        $this->display($tpl);
    }

    /***
     * 修改信息 保存
     * @param $model
     * @param $data
     * @param array $where
     * @param array $msg
     */
    protected function _editData($model, $data, $where = [], $msg = ['success' => '操作成功！', 'error' => '操作失败！', 'url' => '', 'ajax' => IS_AJAX])
    {

        $id = array_unique(explode('_', I('id', 0)));
        if ($id) {
            $where['id'] = array('in', $id);
        }
        $tempModel = D($model);
        if ($tempModel->create()) {
            if ($tempModel->where($where)->save($data) !== false) {
                $this->success($msg['success'], $msg['url'], $msg['ajax']);
            } else {
                $this->error($msg['error'] . '[' . $tempModel->getDbError() . ']', $msg['url'], $msg['ajax']);
            }
        } else {
            $this->error($msg['error'] . '[' . $tempModel->getError() . ']', $msg['url'], $msg['ajax']);
        }

    }

    /***增加信息
     * @param $model
     * @param $data
     * @param array $msg
     */
    protected function _addData($model, $data, $msg = ['success' => '操作成功！', 'error' => '操作失败！', 'url' => '', 'ajax' => IS_AJAX])
    {

        $tempModel = D($model);
        if ($tempModel->create()) {
            $rst = $tempModel->add($data);
            if ($rst) {
                $this->success($msg['success'], $msg['url'], $msg['ajax']);
            } else {
                $this->error($msg['error'] . '[' . $tempModel->getDbError() . ']', $msg['url'], $msg['ajax']);
            }
        } else {
            $this->error($msg['error'] . '[' . $tempModel->getError() . ']', $msg['url'], $msg['ajax']);
        }
    }

    /***
     * 列表数据 分页
     * @param $model
     * @param $where
     * @param int $pageSize
     * @param string $order
     * @return array
     */
    protected function _list($model, $where = '1', $pageSize = 10, $order = 'create_time desc')
    {

        $tempModel = M($model);
        $total = $tempModel->where($where)->count();
        $page = new \Think\Page($total, $pageSize);
        $dataList = $tempModel->where($where)->order($order)->limit($page->firstRow . ',' . $page->listRows)->select();
        $pageHtml = $page->show();
        $empty = '<tr><td colspan="12" class="text-center info">暂无数据</td></tr>';
        return ['dataList' => $dataList, 'pageHtml' => $pageHtml, 'empty' => $empty];
    }

    /***
     * 批量删除
     * @param $model
     */
    protected function _delAll($model)
    {
        $id = I('id', '');
        $idArr = explode('_', $id);
        if (!$idArr) {
            $this->error('缺失参数!');
        } else {
            $where['id'] = array('in', $idArr);
        }
        $data['status'] = 0;
        $tempModel = M($model);
        $rst = $tempModel->where($where)->save($data);
        if ($rst === false) {
            $this->error('操作失败![' . $tempModel->getDbError() . ']');
        } else {
            $this->success('操作成功!');
        }
    }

    /**
     * 获取详情
     * @param $model
     * @param array $data
     * @return mixed
     */
    protected function _detail($model, $data = [])
    {
        $id = I('id', $data['id']);
        $tempModel = M($model);
        $where['id'] = $id;
        return $tempModel->where($where)->find();
    }

    /***
     * 消息展示 传输
     * @param $info
     * @param bool $isAjax
     */
    public function message($info,$status,$isAjax = false)
    {
        if ($isAjax) {
            $this->ajaxReturn(['info'=>$info,'status'=>$status]);
        }
        if ($status == 1) {
            $this->success($info);
        } else {
            $this->error($info);
        }

    }
}